grammar Sample;

options {
  language = Java;
}
@header {
  package gen;
} 
@lexer::header {
  package gen;
} 
evalueter returns [int result]
  : expression EOF {$result=$expression.result;}
  ;
 
term returns [int result]
  :IDENT {$result = 0;}  
  |'('expression')' {$result = $expression.result;}
  |INTEGER{$result = Integer.parseInt($INTEGER.text);}
  ;
 
unary returns [int result]
  :{boolean positive = true;}
    ('+'|'-'{positive = !positive;})* term
    { 
      $result=$term.result;
      if(!positive)
        $result = -$result;
    }
  ; 
 
 mult returns [int result]
  :op1=unary {$result = $op1.result;}
  (
  '*'     op2=unary{$result = $result * $op2.result;}
  |'/'    op2=unary{$result = $result / $op2.result;}
  |'mod'  op2=unary{$result = $result \% $op2.result;}
  )*
  ;

expression returns [int result]
  :op1=mult {$result = $op1.result;}
  ('+' op2=mult{$result = $result + $op2.result;}
  |'-' op2=mult{$result = $result - $op2.result;}
  )*
  ;

INTEGER : '0'..'9'+;
IDENT : ('a'..'z' | 'A'..'Z')('a'..'z' | 'A'..'Z' | '0'..'9' )*; 
WS: (' '|'\t'|'\n'|'\r'|'\f')+ {$channel = HIDDEN;};

rule: ;
